﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace TerminalServis
{
    public class UserData
    {
        SqlTransaction trn = null;
        bool transactionStart = false;
        List<SqlParameter> _params = new List<SqlParameter>();
        public void AddParams(string name, byte sqltype, object value)
        {
            _params.Add(new SqlParameter() { ParameterName = name, SqlDbType = (SqlDbType)sqltype, Value = value });
        }
        public void AddParams(string name, byte sqltype, object value, int size)
        {
            _params.Add(new SqlParameter() { ParameterName = name, SqlDbType = (SqlDbType)sqltype, Value = value, Size = size });
        }
        public void ParamsValue(int idx, object value)
        {
            if (_params[idx].SqlDbType == SqlDbType.Float)
            {
                _params[idx].Value = Cevir.Parse<decimal>(value);
            }
            else
            {
                _params[idx].Value = value;
            }
        }
        public void Exec(string sqlQuery)
        {
            if (transactionStart)
            {
                Database.Get.Exec(sqlQuery, trn, CloneParameters());
            }
            else
            {
                Database.Get.Exec(sqlQuery, null, CloneParameters());
            }
        }

        private SqlParameter[] CloneParameters()
        {
            SqlParameter[] clonedParameters = new SqlParameter[_params.Count];
            int idx = 0;
            foreach (SqlParameter p in _params)
            {
                clonedParameters[idx] = new SqlParameter(p.ParameterName, p.SqlDbType, p.Size) { Value = p.Value };
                idx++;
            }
            return clonedParameters;
        }


        public object ExecuteScaler(string sqlQuery)
        {
            object sonuc = null;
            if (_params.Count > 0)
            {
                sonuc = Database.Get.ExecScalar<object>(sqlQuery, _params.ToArray());
            }
            else
            {
                sonuc = Database.Get.ExecScalar<object>(sqlQuery);
            }
            return sonuc;
        }
        public DataTable Fill(string sqlQuery)
        {
            DataTable dt = Database.Get.Fill(sqlQuery, _params.ToArray());
            return dt;
        }
        public void OpenTransAction()
        {
            transactionStart = true;
            trn = Database.Get.OpenTransaction();
        }
        public bool CommitTran()
        {
            bool sonuc = false;
            if (transactionStart)
            {
                if (trn != null)
                {
                    try
                    {
                        trn.Commit();
                        sonuc = true;
                    }
                    catch (Exception)
                    {
                        if (trn != null)
                        {
                            trn.Rollback();
                        }
                        sonuc = false;
                    }
                    finally
                    {
                        if (trn != null)
                            trn = null;
                    }
                }
                transactionStart = false;
            }
            return sonuc;
        }
        public void Clear()
        {
            _params.Clear();
        }
    }

    public class Hafiza
    {
        private Dictionary<string, UserData> _dict = new Dictionary<string, UserData>();
        private static Hafiza _instance;
        private Hafiza() { }
        public static UserData Get(string sessionID)
        {
            if (_instance == null)
            {
                _instance = new Hafiza();
            }

            if (_instance._dict.ContainsKey(sessionID))
            {
                return _instance._dict[sessionID];
            }
            else
            {
                _instance._dict.Add(sessionID, new UserData());
                return _instance._dict[sessionID];
            }
        }
    }
}
